﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Threading;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_AfficheInformSetting_Index : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            this.DataList_DataTable();
        }
    }

    private void DataList_DataTable() {
        DataTable dt = new DataTable();
        dt.Columns.Add("AfficheInformId", typeof(string));
        dt.Columns.Add("AfficheInformTitle", typeof(string));
        dt.Columns.Add("AfficheInformCreateTime", typeof(string));
        dt.Columns.Add("AfficheInformStartDate", typeof(string));
        dt.Columns.Add("AfficheInformEndDate", typeof(string));
        dt.Columns.Add("AfficheInformState", typeof(string));
        dt.Columns.Add("AfficheInformOpenBound", typeof(string));
        dt.Columns.Add("AfficheInformOpenBoundSnames", typeof(string));
        dt.Columns.Add("AfficheInformSendUser", typeof(string));
        AfficheInformtbService aiService = new AfficheInformtbService();
        List<AfficheInformtb> list = aiService.findALL();
        if(list != null && list.Count > 0) {
            foreach(AfficheInformtb ai in list) {
                DataRow dr = dt.NewRow();
                dr["AfficheInformId"] = ai.AfficheInformId.ToString();
                dr["AfficheInformTitle"] = ai.AfficheInformTitle;
                dr["AfficheInformCreateTime"] = ai.AfficheInformCreateTime.ToString();
                dr["AfficheInformStartDate"] = ai.AfficheInformStartTime.ToShortDateString();
                dr["AfficheInformEndDate"] = ai.AfficheInformEndTime.ToShortDateString();
                string state = "<font color='red'><b>终止</b></font>";
                if(DateTime.Now.Date >= ai.AfficheInformStartTime && DateTime.Now.Date < ai.AfficheInformEndTime.Date) {
                    state = "<font color='green'><b>有效</b></font>";
                }
                dr["AfficheInformState"] = state;
                dr["AfficheInformOpenBound"] = ai.AfficheInformOpenBound;
                dr["AfficheInformOpenBoundSnames"] = ai.AfficheInformOpenBoundSnames;
                dr["AfficheInformSendUser"] = ai.AfficheInformSendUser.UserName;
                dt.Rows.Add(dr);
            }
            this.DataList1.DataSource = dt.DefaultView;
        } else {
            this.DataList1.DataSource = null;
        }
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e) {
        if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) {
            string state = ((Label)e.Item.FindControl("LblState")).Text;
            if("<font color='red'><b>终止</b></font>".Equals(state)) {
                ((LinkButton)e.Item.FindControl("LbtZhongZhi")).Visible = false;
            } else {
                ((LinkButton)e.Item.FindControl("LbtShengXiao")).Visible = false;
            }
        }
    }
    protected void LbtDel_Click(object sender, EventArgs e) {
        int ai_id = int.Parse(((LinkButton)sender).Attributes["ai_id"]);
        AfficheInformtbService aiService = new AfficheInformtbService();
        if(aiService.delAfficheInformInfoById(ai_id) == 1) {
            this.DataList_DataTable();
        } else {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('删除失败，请重试！');", true);
        }
    }
    protected void Button1_Click(object sender, EventArgs e) {
        Response.Redirect("AddAfficheInform.aspx");
    }
    protected void LbtEdit_Click(object sender, EventArgs e) {
        string ai_id = ((LinkButton) sender).Attributes["ai_id"];
        Response.Redirect("UpdateAfficheInformInfo.aspx?ai_id=" + ai_id);
    }
    protected void LbtShengXiao_Click(object sender, EventArgs e) {
        int ai_id = int.Parse(((LinkButton) sender).Attributes["ai_id"]);
        AfficheInformtbService aiService = new AfficheInformtbService();
        if(aiService.toGoOnByAiid(ai_id) == 1) {
            this.DataList_DataTable();
        }
    }
    protected void LbtZhongZhi_Click(object sender, EventArgs e) {
        int ai_id = int.Parse(((LinkButton) sender).Attributes["ai_id"]);
        AfficheInformtbService aiService = new AfficheInformtbService();
        if(aiService.toStopByAiid(ai_id) == 1) {
            this.DataList_DataTable();
        }
    }
    protected void LbtChaYue_Click(object sender, EventArgs e) {
        int ai_id = int.Parse(((LinkButton) sender).Attributes["ai_id"]);
        Response.Redirect("AfficheInformConsult.aspx?ai_id=" + ai_id);
    }
}
